import {
  productDetail
} from "../../../util/api/product.js";
import {
  imgUrl
} from "@/util/api.js";
import {
  getImage
} from "@/util/getImage.js";
import {
  isWeixin,
  wxShareConfig
} from "@/util/wx.js";
export default {
  data() {
    return {
      id: '',
      detailInfo: {
        productDetailImage: []
      },
      imgUrl: imgUrl
    };
  },
  onLoad(e) {
    this.id = e.id
    this.getDetail()
  },
  methods: {
    // 预览轮播图
    previewImage(index) {
      let list = this.detailInfo.productDetailImage.map(item => {
        if (item.includes('https')) {
          return item
        } else {
          return this.imgUrl + item
        }
      });
      WeixinJSBridge.invoke("imagePreview", {
        urls: list,
        current: index
      });
    },
    getImageUrl(url) {
      return getImage(url);
    },
    getDetail() {
      productDetail(this.id).then(res => {
        if (res.code === 200) {
          this.detailInfo = res.result
          if (this.detailInfo.productName) {
            if (isWeixin()) {
              this.initShareConfig();
            }
          }
        }
      })
    },
    onBuy() {
      let token = this.$getToken();
      if (!token) {
        uni.showToast({
          title: "您还没有登录，请登录后操作",
          duration: 2000,
          icon: "none"
        });
        setTimeout(() => {
          uni.setStorageSync("pageUrl", window.location.href);
          uni.navigateTo({
            url: "/pages/login/index"
          });
        }, 2000);
        return;
      } else {
        uni.navigateTo({
          url: `/pages/product/productPay/Index?id=${this.id}`
        });
      }
    },
    // 微信分享
    initShareConfig() {
      let _this = this;
      let windowurl = window.location.href.split("#")[0];
      uni.request({
        url: "https://oldm.51bmj.cn/WeiXin/GetClientSign", //仅为示例，并非真实接口地址。
        data: {
          rawUrl: windowurl
        },
        dataType: "json",
        header: {
          "X-Requested-With": "XMLHttpRequest"
        },
        method: "GET",
        success: res => {
          if (res.statusCode == 200) {
            let {
              data
            } = res;
            let shareInfo = {
              title: '比目镜',
              desc: _this.detailInfo.productName,
              link: window.location.href
            };
            wxShareConfig(this.$wx, data, shareInfo);
          }
        }
      });
    },
  }
};